package panels.util;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JPanel;


public class JFileChooserButton extends JPanel implements PropertyChangeListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 4562765952579290685L;
	
	public static final String DIALOG_BUTTON_TEXT_PROPERTY = "DIALOG_BUTTON_TEXT_PROPERTY";
	public static final String SELECTED_FILE_PROPERTY = "SELECTED_FILE_PROPERTY";
	public static final String BUTTON_TEXT_PROPERTY = "BUTTON_TEXT_PROPERTY";
	public static final String CHANGED_PROPERTY = "CHANGED_PROPERTY";
	
	private JButton dialogButton = new JButton("Choose File");
	private File	selectedFile = null;
	private String	dialogButtonText = "Select";
	
	private static final boolean myEquals(Object A, Object B) {
		if (A==null) {
			return (B==null);
		} else {
			return (B!=null && A.equals(B));
		}
	}
	
	public JFileChooserButton() {
		setLayout(new BorderLayout(0, 0));
		
		dialogButton = new JButton("");
		dialogButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JFileChooser	chooser = new JFileChooser();
				chooser.setCurrentDirectory(getSelectedFile());
				chooser.showDialog((Component) e.getSource(), getDialogButtonText());
				setSelectedFile(chooser.getSelectedFile());
				
			}
		});
		add(dialogButton);
		
		
		this.putClientProperty(SELECTED_FILE_PROPERTY, null);
		this.putClientProperty(BUTTON_TEXT_PROPERTY, getButtonText());
		this.putClientProperty(DIALOG_BUTTON_TEXT_PROPERTY, getDialogButtonText());
		
		this.addPropertyChangeListener(this);
	}

	public String getButtonText() {
		
		return dialogButton.getText();
	}

	public void setButtonText(String newValue) {
		Object oldValue = this.getClientProperty(BUTTON_TEXT_PROPERTY);

		this.dialogButton.setText(newValue);
		if (!myEquals(oldValue,newValue)) {
			this.firePropertyChange(BUTTON_TEXT_PROPERTY, oldValue, newValue);
		}
	}

	public File getSelectedFile() {
		return selectedFile;
	}

	public void setSelectedFile(File newValue) {
		Object oldValue = this.getClientProperty(SELECTED_FILE_PROPERTY);

		this.selectedFile = newValue;
		if (!myEquals(oldValue,newValue)) {
			this.firePropertyChange(SELECTED_FILE_PROPERTY, oldValue, newValue);
		}
	}

	public String getDialogButtonText() {
		return dialogButtonText;
	}

	public void setDialogButtonText(String newValue) {
		Object oldValue = this.getClientProperty(DIALOG_BUTTON_TEXT_PROPERTY);
		this.dialogButtonText = newValue;
		if (!myEquals(oldValue,newValue)) {
			this.firePropertyChange(DIALOG_BUTTON_TEXT_PROPERTY, oldValue, newValue);
		}
	}

	@Override
	public void propertyChange(PropertyChangeEvent evt) {
		if (!evt.getPropertyName().equals(CHANGED_PROPERTY)) {
			this.firePropertyChange(CHANGED_PROPERTY, false, true);
		}
	}
	


}
